﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace GameProject
{
    /// <summary>
    /// Animations klass som gör det möjligt att
    /// skapa animationen när kuben crashar.
    /// </summary>
    class animation  
    {
        Texture2D spriteStrip;
        float scale;
        int elapsedTime;
        int frameTime;
        int frameCount;
        int currentFrame;
        Color color;
        Rectangle sourceRect = new Rectangle();
        Rectangle destinationRect = new Rectangle();
        public int FrameWidth;
        public int FrameHeight;
        public bool Active;
        public bool Looping;
        public Vector2 Position;

        /// <summary>
        /// Initiation.
        /// </summary>
        /// <param name="texture"></param>
        /// <param name="position"></param>
        /// <param name="frameWidth"></param>
        /// <param name="frameHeight"></param>
        /// <param name="frameCount"></param>
        /// <param name="frametime"></param>
        /// <param name="color"></param>
        /// <param name="scale"></param>
        /// <param name="looping"></param>
        public void Initialize(Texture2D texture, Vector2 position, int frameWidth
                               ,int frameHeight, int frameCount, int frametime
                               ,Color color, float scale,bool looping)
        {
            this.color = color;
            this.FrameWidth = frameWidth;
            this.FrameHeight = frameHeight;
            this.frameCount = frameCount;
            this.frameTime = frametime;
            this.scale = scale;

            Looping = looping;
            Position = position;
            spriteStrip = texture;

            elapsedTime = 0;
            currentFrame = 0;

            Active = true;
        }

        /// <summary>
        /// Uppdaterar animationen.
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            // Updaterar inte ifall vi inte är aktiva
            if (Active == false)
                return;

            // Uppdaterar den förlutna tiden
            elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;

            // Om den förflutna tiden är större frame tiden 
            // behövs det byta frame
            if (elapsedTime > frameTime)
            {
                currentFrame++;
                
                if (currentFrame == frameCount)
                {
                    currentFrame = 0;
                    if (Looping == false)
                        Active = false;
                }

                elapsedTime = 0;
            }

            sourceRect = new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight);

            destinationRect = new Rectangle((int)Position.X - (int)(FrameWidth * scale) / 2,
            (int)Position.Y - (int)(FrameHeight * scale) / 2,
            (int)(FrameWidth * scale),
            (int)(FrameHeight * scale));
        }

        /// <summary>
        /// Målar ut animationen.
        /// </summary>
        /// <param name="spriteBatch"></param>
        public void Draw(SpriteBatch spriteBatch)
        {
            if (Active)
            {
                spriteBatch.Draw(spriteStrip, destinationRect, sourceRect, color);
            }
        }
    }
}
